<script>
	import {
		Decrypt,
		Encrypt
	} from "@/utils/crypto"
	import wsRequest from "@/utils/websocketPlus.js"
	import {
		mapMutations,
		mapState
	} from "vuex";
	export default {
		data() {
			return {
				websock: null,
				wsRequest: null,
				socketTask: null,
				socketTaskIsOpen: false,
				addList: [],
				time: 0,
				value: ''
			}
		},
		computed: {
			...mapState('user', ["userInfo", "deleteObj", "alertList", 'notifyObject']),
		},
		// onShow() {
		// 	if (this.addList?.length > 0) {
		// 		plus.push.addEventListener("click", (msg) => {
		// 			// this.set_gifArray(this.addList)
		// 			console.log("click:" + JSON.stringify(msg));
		// 			console.log(this.addList, '数组');
		// 			this.value = msg
		// 			console.log(this.addList[this.value.payload.value - 1], '111');
		// 			var map = new Map();
		// 			const dataList = this.addList.filter(a => {
		// 				if (!map.has(a.bedid)) {
		// 					return map.set(a.bedid, 1)
		// 				}
		// 			})
		// 			let user = uni.getStorageSync('userInfo')
		// 			console.log(user);
		// 			console.log(dataList[this.value.payload.value - 1], '222');
		// 			this.set_gifArray(dataList[this.value.payload.value - 1])
		// 			uni.navigateTo({
		// 				url: `../hospitallist/index?value=` + true
		// 			})
		// 		}, false);
		// 	}
		// },

		onLaunch: function() {
			let that = this
			uni.$on('message', function(data) {
				that.websocketonmessage(data)
			})
		},
		watch: {
			userInfo: {
				handler(newval) {
					if (newval?.HosKind && newval.sPassword) {
						this.initWebSocket()
					} else {
						this.wsRequest.disconnect();
					}
				},
				deep: true,
				
			}
		},
		methods: {
			...mapMutations("user", ["set_gifArray", "set_alertList", 'set_NOTIFY']),
			initWebSocket() {
				let user = uni.getStorageSync('userInfo')
				this.wsRequest = new wsRequest(
					`ws://20.21.9.38:8022/smartcare/ws?hoscode=${user.hoscode}&deptId=${user.HosKind}&authorizationUserId=${user.sUserid}&type=mobile`,
					['2b2909e153cfc8d19096dfbc5a95c7f3'])
				// this.wsRequest = new wsRequest(
				// 	`ws://192.168.31.227:8022/smartcare/ws?hoscode=${user.hoscode}&deptId=${user.HosKind}&authorizationUserId=${user.sUserid}`,
				// 	['2b2909e153cfc8d19096dfbc5a95c7f3'])
			},
			websocketonmessage(data) {
			
				try {
					//数据接收
					let DeCryData = Decrypt(data)
	
					const {
						user
					} = JSON.parse(DeCryData)
					
					// var x2js = new X2JS();
					// const {
					// 	User
					// } = x2js.xml2js((DeCryData));
					// console.log(user, '接受到的数据');
					if (!user) {
						return
					}
					
				
					user?.iconTbladdList ? this.webcreateMessage(user) : ''

				} catch (e) {
					//TODO handle the exception
				}
			},
			webcreateMessage(User) {
				// console.log('App Launch')
				let {
					iconMain,
					iconTbladdList,
					remark
				} = User

				const deleteArr = uni.getStorageSync("deleteArr")
				let user = uni.getStorageSync('userInfo')
				if( iconTbladdList.find(({
							fldcode
						}) => fldcode === 'pt_callsys')?.inputfield){
					console.log(deleteArr,'deleteArr');
					console.log(biliid,'biliid1');
					console.log(user,'user')
				}
     
				const biliid = deleteArr.filter(item => {
					return item.BEDID == iconMain.bedid
				})

				if (biliid.length > 0) {
					if ((biliid[0].NURSE1 == (user.sUserid).toUpperCase()) || (biliid[0].NURSE2 ==( user.sUserid).toUpperCase())) {
						const iv_sys = iconTbladdList.find(({
							fldcode
						}) => fldcode === 'iv_sys')?.inputfield;
						// console.log(iv_sys, 'iv_sys');
						const pt_callsys = iconTbladdList.find(({
							fldcode
						}) => fldcode === 'pt_callsys')?.inputfield;
						if (iv_sys == 2) { //输液报警
							if (!this.notifyObject['iv_sys'][iconMain.iconCode]) {
								this.notifyObject['iv_sys'][iconMain.iconCode] = {
									roomid: iconMain.roomid,
									bedid: iconMain.bedid,
									remark: iconTbladdList.find(({
										fldcode
									}) => fldcode === 'iv_sys').remark,
									iconCode: iconMain.iconCode,
									modifydate: iconTbladdList.find(({
										fldcode
									}) => fldcode === 'iv_sys').modifydate,
									title: '输液报警'
								}
								// console.log(this.notifyObject, 'this.notifyObject');
								this.set_NOTIFY(JSON.parse(JSON.stringify(this.notifyObject)))
								this.messgetFun()
							}
						} else if ((iv_sys == 1 || iv_sys == 0) && this.notifyObject['iv_sys'][iconMain.iconCode]) {
							delete this.notifyObject['iv_sys'][iconMain.iconCode]
							this.set_NOTIFY(JSON.parse(JSON.stringify(this.notifyObject)))
						}
						console.log(pt_callsys,'ptcall');
						if (pt_callsys == 1) { //呼叫报警
						
							if (!this.notifyObject['pt_callsys'][iconMain.iconCode]) {
								console.log('kaiqi');
								this.notifyObject['pt_callsys'][iconMain.iconCode] = {
									roomid: iconMain.roomid,
									bedid: iconMain.bedid,
									remark: iconTbladdList.find(({
										fldcode
									}) => fldcode === 'pt_callsys').remark,
									iconCode: iconMain.iconCode,
									modifydate: iconTbladdList.find(({
										fldcode
									}) => fldcode === 'pt_callsys').modifydate,
									title: '呼叫报警'
								}
								console.log('报警');
								this.set_NOTIFY(JSON.parse(JSON.stringify(this.notifyObject)))
								this.messgetFun()
							}
						} else if (pt_callsys == 0 && this.notifyObject['pt_callsys'][iconMain.iconCode]) {
							delete this.notifyObject['pt_callsys'][iconMain.iconCode]
							this.set_NOTIFY(JSON.parse(JSON.stringify(this.notifyObject)))
						}
					}
				}
				// let add = data.iconTbladdList.map((item, index) => {
				// 	if (item.fldcode == 'iv_sys' || item.fldcode == 'pt_callsys') {
				// 		if (item.fldcode == 'iv_sys') {
				// 			if (item.inputfield == 2) {
				// 				payload.content = `您有条输液报警信息请查看！`
				// 				return {
				// 					roomid: data.iconMain.roomid,
				// 					bedid: data.iconMain.bedid,
				// 					remark: item.remark,
				// 					iconCode: item.iconCode,
				// 					modifydate: item.modifydate,
				// 					title: '输液报警'
				// 				}
				// 			} else {
				// 				for (let i = 0; i < this.alertList.length; i++) {
				// 					if (this.alertList[i].bedid == item.bedid && this.alertList[i].iconCode == item
				// 						.iconCode && this.alertList[i].roomid == item.roomid) {
				// 						this.alertList.splice(i, 1)
				// 						this.set_alertList(this.alertList)
				// 					}
				// 				}
				// 				return undefined
				// 			}

				// 		} else if (item.fldcode == 'pt_callsys') {
				// 			if (item.inputfield == 1) {
				// 				payload.content = `您有条呼叫报警信息请查看！`
				// 				return {
				// 					roomid: data.iconMain.roomid,
				// 					bedid: data.iconMain.bedid,
				// 					remark: item.remark,
				// 					iconCode: item.iconCode,
				// 					modifydate: item.modifydate,
				// 					title: '呼叫报警'
				// 				}
				// 			} else {
				// 				for (let i = 0; i < this.alertList.length; i++) {
				// 					if (this.alertList[i].bedid == item.bedid && this.alertList[i].iconCode == item
				// 						.iconCode && this.alertList[i].roomid == item.roomid) {
				// 						this.alertList.splice(i, 1)
				// 						this.set_alertList(this.alertList)
				// 					}
				// 				}
				// 				return undefined
				// 			}

				// 		} else {
				// 			return []
				// 		}
				// 	}
				// })
				// if (payload?.content) {
				// 	add = add.filter(n => n);
				// 	const deleteArr = uni.getStorageSync("deleteArr")
				// 	let user = uni.getStorageSync('userInfo')
				// 	if (add.length > 0 || !add) {
				// 		const biliid = deleteArr.filter(item => {
				// 			return item.BEDID == add[0].bedid
				// 		})
				// 		console.log(biliid, 'biliid');
				// 		let bilstShow = false;
				// 		if (biliid.length > 0) {
				// 			if (biliid[0].NURSE1 == user.sUserid) {
				// 				// this.messgetFun()
				// 				this.addList.push(add[0])
				// 				bilstShow = true
				// 			} else if (biliid[0].NURSE2 == user.sUserid) {
				// 				// this.messgetFun()
				// 				this.addList.push(add[0])
				// 				bilstShow = true
				// 			} else {
				// 				bilstShow = false
				// 				return
				// 			}
				// 			if (bilstShow) {
				// 				this.addList = this.addList.filter(n => n);
				// 				let ass = this.addList.reduce((prev, cur, index, arr) => {
				// 					if (prev.filter(v => v.bedid == cur.bedid && v.title === cur.title).length >
				// 						0) {
				// 						return prev
				// 					} else {
				// 						return [...prev, cur]
				// 					}
				// 				}, [])
				// 				this.messgetFun()
				// 				this.set_alertList(ass)
				// 			}
				// 		}
				// 	}
				// }


			},
			//手机震动
			messgetFun() {
				uni.vibrateLong({
					success: function() {
						plus.device.vibrate(500);
					}
				});
				let main = plus.android.runtimeMainActivity();
				let RingtoneManager = plus.android.importClass("android.media.RingtoneManager");
				let uri = RingtoneManager.getActualDefaultRingtoneUri(main, RingtoneManager
					.TYPE_NOTIFICATION);
				let MediaPlayer = plus.android.importClass("android.media.MediaPlayer");
				let player = MediaPlayer.create(main, uri);
				player.setLooping(false);
				player.prepare();
				player.start();
			}
		},

		onShow: function() {
			
		},
		mounted() {
			
		},
	
		// onHide: function() {
		// 	console.log('App Hide')
		// }
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "./assets/iconfont/iconfont.css";

	::v-deep .uni-modal .uni-modal__bd {
		font-size: 18px !important;
		display: flex;
		justify-content: center;
	}

	::v-deep .uni-toast {
		width: 40% !important;

		.uni-toast__content {
			padding: 5px 10px;
		}
	}
</style>
